/* Solstice Theme by Jake Paul http://jakepaul.tumblr.com */
/* Endless scrolling code adapted from MetaLab's Fluid theme (http://fluidtheme.tumblr.com/) */
/* Twitter integration based on Hayden Hunter's Prologue theme (http://prologuetheme.tumblr.com/) */
/* Lightbox with FancyBox http://fancybox.net/ */

$().ready(function() {

	date_setup();
	show_zoom();
	zoom_setup();
	insert_credit();
	if (typeof disqus_shortname != 'undefined') { comment_count(); };
	
	var start_page = $("#hidden .startpage").text();	
	var next_page = parseInt(start_page) + 1;
	var total_pages = $("#hidden .totalpages").text();	
	var loading_next_page = false;
	var new_posts = "";
	
	if ((ajax_loader === true) && 
		(navigator.userAgent.toLowerCase().match(/iPhone/i) != "iphone" ) && 
		(navigator.userAgent.toLowerCase().match(/iPod/i) != "ipod" ) )	{
			$("#classic").hide();
			$("#endless").show();
			$("#endless #more").bind("click", function(){newPosts();});
	}		
	
	function newPosts(){
		if ( (next_page <= total_pages) && (! loading_next_page) ){
			loading_next_page = true;
			$("#endless #more .label").hide();
			$("#endless #more .spinner").show();
			jQuery.get("/page/" + next_page + "?cb=" + Math.random(), function(data){
				new_posts = data.split('<!-- posts-start -->')[1].split('<!-- posts-end -->')[0];
				$("#posts_inner").append(new_posts);
				next_page++;
				loading_next_page = false;
				$("#endless #more .spinner").hide();
				$("#endless #more .label").show();
				if (next_page > total_pages) { $("#endless").hide(); };  // Hide the button if we run out of pages 
				date_setup();
				show_zoom();
				zoom_setup();
				if (typeof disqus_shortname != 'undefined') { comment_count(); };
		  	});
		}	
	}

});

function comment_count() {
  var query = '?';
  var disqus_links = [];
	$("a").each(function(index){
	  if ($(this).hasClass('disqus')) {
	    disqus_links.push($(this));
	    query += 'url' + index + "=" + encodeURIComponent($(this).attr('href')) + '&';
    }
	});	
	jQuery.getScript('http://disqus.com/forums/' + disqus_shortname + '/get_num_replies.js' + query, function() {
	  if (hide_zero) {
  	  $("a.disqus").filter(function(){
        return $(this).html() == "0 Comments";
      }).hide();
    }
	});
}

function date_setup() {
	$('.type').hover(
      function() {
		$(this).parent().parent().find(".date").show()
      },
      function() {
		$(this).parent().parent().find(".date").hide()
      }
	);
}

function show_zoom() {
	$('.image').hover(
	  function() {
		$(this).find(".zoom").show();
      },
      function() {
		$(this).find(".zoom").hide();
      }
	);
}

function zoom_setup() {
	$("a.zoom").fancybox({'overlayColor': '#000', 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true, 'centerOnScroll': false }); 
}

function insert_credit() {
	$('#container_inner').append('<div id="credit">&copy; 2010 - <a href="http://leolink.net">LeoLink&reg;</a></div>');
}

// Stolen from Twitter Blogger Badge
// http://twitter.com/badges/blogger
function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
        return 'Less than a minute ago';
    } else if (delta < 120) {
        return 'A minute ago';
    } else if (delta < (60 * 60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (120 * 60)) {
        return 'An hour ago';
    } else if (delta < (24 * 60 * 60)) {
        return (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}

// Parsing Strings With JQuery
// http://devkick.com/blog/parsing-strings-with-jquery
$.fn.clickUrl = function() {
    var regexp = /((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g;
	var regexpb = /\B@([_a-z0-9]+)/ig;
    this.each(function() {
        $(this).html(
        	$(this).html().replace(regexp, '<a href="$1">$1</a>')
		);
		$(this).html(
			$(this).html().replace(regexpb, '<a href="http://twitter.com/$1">@$1</a>')
        );
    });
    return $(this);
}

function recent_tweets(data) {
    for (i=0; i<3; i++) {
		$("#tweets").append('<li><span class="tweet">' + data[i]['text'] + 
		'</span><a href="http://twitter.com/' + data[i]['user']['screen_name'] + '/status/' +  data[i]['id'] + '" class="tweet_time">' + 
		relative_time(data[i]['created_at']) + '</a></li>');
    }
	$(".tweet").clickUrl();
    $("#twitter").show();
}